﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using Model;
using EncryptHelper;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace IYAKASAPortal.Services
{
    public static class UserAuthority
    {
        public static UserInfo GetUserAuthority(string loginName)
        {
            UserInfo userInfo = new UserInfo();

            EMPLOYEE_USER_BLL bll = new EMPLOYEE_USER_BLL();
            EMPLOYEE_USER[] users = bll.GetModelList("EMPLOYEE_ID='" + loginName + "'");

            userInfo.UserID = users[0].EMPLOYEE_ID_NO;
            userInfo.UserPower = users[0].EMPLOYEE_POWER;
            userInfo.UserName = users[0].EMPLOYEE_NAME;
            userInfo.UserRole = users[0].LICENSE_NAME;

            return userInfo;
        }
    }

    public class UserInfo
    {
        public string UserName {get;set;}
        public string UserPower{get;set;}
        public string UserID {get;set;}
        public string UserRole { get; set; }
    }

    public class UDPHelper
    {
        private UdpClient client;
        private string CIPAddress {get;set;}
        private int Port { get; set; }
        public UDPHelper(string strIPAddress, int Port)
        {
            InitUPD(strIPAddress, Port);
        }

        private void InitUPD(string strIPAddress,int Port)
        {
            CIPAddress = strIPAddress;
            this.Port = Port;
            client = new UdpClient(strIPAddress, Port);
        }

        public bool SendDataToLED(string content, int lineNum)
        {
            for(int i=0;i<3;i++)
            {
                bool b =  SendData(content, lineNum);
                if (b && GetData(lineNum))
                {
                    return b;
                }
            }

            return false;
        }

        private bool SendData(string content,int lineNum)
        {
            try
            {
                byte[] bcon = Encoding.GetEncoding("GB2312").GetBytes(content);
                byte[] b = new byte[bcon.Length + 4];
                b[0] = 0x1B;
                b[1] = byte.Parse(lineNum.ToString());
                b[2] = byte.Parse(bcon.Length.ToString());
                for (int i = 3; i < bcon.Length + 3; i++)
                {
                    b[i] = bcon[i - 3];
                }
                b[b.Length - 1] = 0x0D;
                client.Send(b, b.Length);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        private bool GetData(int lineNum)
        {
            string strRet = "";
            try
            {
                IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
                DateTime dtStart = DateTime.Now;
                while (dtStart.AddSeconds(1) > DateTime.Now)
                {
                    byte[] b = client.Receive(ref remotePoint);
                    if (b.Length > 0)
                    {
                        if (b[0] == 0x1B && b[b.Length - 1] == 0x0D && Convert.ToInt32(b[1])== lineNum)
                        {
                            byte[] bCon = new byte[b[2]];
                            for (int i = 0; i < b[2]; i++)
                            {
                                bCon[i] = b[i + 3];
                            }

                            strRet = Encoding.GetEncoding("GB2312").GetString(bCon);
                            if (strRet == "成功")
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return false;
        }
    }
}